package cn.nange.cmfz.service.impl;

import cn.nange.cmfz.dao.UserDao;
import cn.nange.cmfz.entity.User;
import cn.nange.cmfz.service.UserService;
import cn.nange.cmfz.util.ThreadPoolUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    /**
     * Logger LoggerFactory 都是org.slf4j包的
     * 参数是当前类的类对象
     */
    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserDao userDao;

    @Override
    public void register(User u) {
        userDao.insert(u);
    }

    @Override
    public void update(User u) {
        userDao.update(u);
    }

    @Override
    public void delete(Integer id) {
        userDao.delete(id);
    }

    @Override
    public void ban(Integer id) {
        userDao.ban(id);
    }

    @Override
    public User showOne(Integer id) {
        return userDao.selectOne(id);
    }

    @Override
    public User login(String name) {
        return userDao.selectByName(name);
    }

    @Override
    public PageInfo<User> showAll(int page, int rows) {
        PageHelper.startPage(page, rows);
        PageInfo<User> pageInfo = new PageInfo<>(userDao.selectAll());
        return pageInfo;
    }

    @Override
    public List<Map> showAllBySex() {
        return userDao.selectAllBySex();
    }

    @Override
    public List<Map> showAllByProvince() {
        return userDao.selectAllByProvince();
    }

    @Override
    public Map showByDayCount() {
        Map map = new HashMap();
        map.put("one",userDao.selectByDayCount(365,1));
        map.put("two",userDao.selectByDayCount(720,365));
        map.put("three",userDao.selectByDayCount(9900,720));
        return map;
    }

    @Override
    public Map showByDay() {
        Map map = new HashMap();
        map.put("one",userDao.selectByDay(30,1));
        map.put("two",userDao.selectByDay(90,30));
        map.put("three",userDao.selectByDay(360,90));
        return map;
    }

    @Override
    public Map showByThread() throws InterruptedException {

        Map map = new HashMap();

        CountDownLatch countDownLatch = new CountDownLatch(3);

        System.out.println("创建三个线程");
        logger.debug("创建三个线程");
        logger.info("创建三个线程");
        logger.warn("创建三个线程");
//        logger.error("创建三个线程");
        System.out.println("哈哈哈哈啊哈哈");

//        ThreadPoolExecutor executor = ThreadPoolUtil.executor;
//
//        executor.submit(new Runnable() {
//            @Override
//            public void run() {
//                //                注册量
//                Map map1 = showByDayCount();
//                map.put("count",map1);
//                System.out.println("注册量");
////                计数器减一
//                countDownLatch.countDown();
//            }
//        });

        ThreadPoolExecutor executor = ThreadPoolUtil.executor;

        executor.submit(new Runnable() {
            @Override
            public void run() {
                //                注册量
                Map map1 = showByDay();
                map.put("count",map1);
                System.out.println("注册量");
//                计数器减一
                countDownLatch.countDown();
            }
        });

        executor.submit(new Runnable() {
            @Override
            public void run() {
                //                男女人数
                List<Map> maps = showAllBySex();
                map.put("sex",maps);
                System.out.println("男女人数");
                //                计数器减一
                countDownLatch.countDown();
            }
        });

        executor.submit(new Runnable() {
            @Override
            public void run() {
                //                地区分布
                List<Map> maps = showAllByProvince();
                map.put("province", maps);
                System.out.println("地区分布");
                //                计数器减一
                countDownLatch.countDown();
            }
        });


//        2.让主线程等待子线程执行结束
        countDownLatch.await();

        return map;
    }

}
